<template>
  <div class="app-container">
    <el-button class="editor" size="mini" type="success" @click="contractUpdate">编辑</el-button>
    <div class="nav-search">
      <el-input class="input" v-model="searchParams.code" placeholder="合同编号"></el-input>
      <el-select v-model="searchParams.contractType" placeholder="合同类型">
        <el-option
          v-for="(item,index) in dict.package_rule_type"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input class="input" v-model="searchParams.serachStr" placeholder="诊所名称"></el-input>
      <el-input v-model="searchParams.orgId" placeholder="机构名称"></el-input>
       <el-select v-model="searchParams.packageRuleType" placeholder="所属区域">
        <el-option
          v-for="(item,index) in dict.package_rule_type"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input class="input" v-model="searchParams.userId" placeholder="所属销售"></el-input>
       <el-select v-model="searchParams.stat" placeholder="合同状态">
        <el-option
          v-for="(item,index) in dict.package_rule_type"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
       <el-select v-model="searchParams.packageRuleType" placeholder="是否签合同">
        <el-option
          v-for="(item,index) in dict.package_rule_type"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-date-picker
        v-model="searchParams.createDate"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="签署时间"
      >
      </el-date-picker>
      <div class="btn">
        <el-button class="ebtn" size="mini"  @click="toQuery">搜索</el-button>
        <el-button class="ebtn reset" size="mini"  @click="toReset">重置</el-button>
        <!-- <el-button class="filter-item" size="mini" type="success" @click="addRules">新增</el-button> -->
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="tableData"
        size="small"
        style="width: 100%;"
      >
        <el-table-column prop="code" label="合同编号"></el-table-column>
        <el-table-column prop="contractType" label="规合同类型">
          <template slot-scope="scope">
            {{ getDictName(scope.row.ruleType,dict.package_rule_type) }}
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="合同所属销售"></el-table-column>
     
        <el-table-column prop="groupCount" label="机构名称"></el-table-column>
        <el-table-column prop="clinicCount" label="诊所名称"></el-table-column>
        <el-table-column prop="ruleName" label="所属区域"></el-table-column>

        <el-table-column label="是否签合同">
          <template slot-scope="scope">
            <span>{{ scope.row.enabled == 1 ? "已签" : "未签" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="签约时间" width="140px">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createDate) }}
          </template>
        </el-table-column>
         <el-table-column prop="begin" label="合同开始时间" width="140px">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="end" label="合同结束时间" width="140px">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createDate) }}
          </template>
        </el-table-column>
            <el-table-column label="合同状态">
          <template slot-scope="scope">
            <span>{{ scope.row.stat == 1 ? "启用" : "停用" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remainingDay" label="剩余天数" width="140px">
          <template slot-scope="scope">
            {{ scope.row.createDate }}天
          </template>
        </el-table-column>
         <el-table-column prop="createDate" label="备注" width="140px">
          <template slot-scope="scope">
            {{ scope.row.createDate }}天
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button v-if="scope.row.enabled == '1'" size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">停用</el-button>
            <el-button v-else size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">启用</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">备注</el-button>
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <div class="dialog">
      <el-dialog
        :visible.sync="dialogVisible"
        width="30%"
        append-to-body
        :modal="false"
      >
        <span slot="title" class="dialog-title">{{ dialogTitle }}</span>
        <productRulesDialog :callback="callback" :row-id="rowId"></productRulesDialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { contractQuery,contractUpdate } from '@/api/oper';
import { mapGetters } from 'vuex';
import { parseTime, getDictName } from '@/utils/index';
import productRulesDialog from '@/views/oper/contractList/productRulesDialog';

export default {
  components: {
    productRulesDialog
  },
  dicts: ['package_rule', 'package_rule_type'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogTitle: '',
      searchParams: {
        createDate: '', // 创建时间
        packageRuleType: '', // 规则类型 dict=package_rule_type
        serachStr: '' // 搜索信息
      },
      page: {
        page: 0,
        size: 10
      },
      tableData: [],
      total: 0,
      rowId: ''
    };
  },
  computed: {
    ...mapGetters(['searchInfo', 'user'])
  },
  watch: {},
  mounted() {
    this.contractQuery({ pageIndex: 0, pageSize: 10 });
    console.log('dict.package_rule_type----', this.dict.package_rule_type);
    // let a = this.getDictName("01",this.dict.package_rule_type);
    // console.log("获取字典值了！！！",a);
  },
  methods: {
    parseTime,
    getDictName,
    // 获取产品规则列表
    contractQuery(params) {
      this.loading = true;
      contractQuery(params)
        .then(res => {
          console.log('hellow nihao', res);
          this.tableData = res.content || [];
          this.total = res.totalElements || 0;
          this.loading = false;
        })
        .catch(er => {
          this.loading = false;
        });
    },
    contractUpdate(){
      contractUpdate().then((res)=>{
        console.log(res)
      })
    },   // 搜索
    toQuery() {
      const search = {
        pageIndex: this.page.page - 1,
        pageSize: this.page.size,
        ...this.searchParams
      };
      this.contractQuery(search);
    },
    // 重置
    toReset() {
      this.searchParams.createDate = '';
      this.searchParams.packageRuleType = '';
      this.searchParams.serachStr = '';
      this.toQuery();
    },
    // 新增
    addRules() {
      this.dialogVisible = true;
      this.dialogTitle = '新增规则';
      this.rowId = '';
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑规则';
      this.rowId = row.id;
    },
    // 禁用&启用
    handleDelete(index, row) {
      const param = {
        'createBy': row.createBy,
        'enabled': parseInt(row.enabled) === 1 ? 0 : 1,
        'id': row.id,
        'packageIds': row.packageIdList,
        'phase': row.phaseList,
        'productIds': row.productIdList,
        'ruleName': row.ruleName,
        'rules': row.ruleList
      };
      const msg = parseInt(row.enabled) === 1 ? '禁用' : '启用';
      this.$confirm('此操作将' + msg + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        editProductRules(param).then(res => {
          this.$notify({
            title: '提示',
            message: msg + '成功',
            type: 'success',
            duration: 5000
          });
          this.contractQuery({ pageIndex: 0, pageSize: 10 });
        });
      });
    },
    sizeChangeHandler() {
      this.toQuery();
    },
    // 页数变更
    pageChangeHandler(pageIndex) {
      this.toQuery();
    },
    callback(type) {
      if (type === '0') {
        this.dialogVisible = false;
      } else if (type === '1') {
        this.dialogVisible = false;
        this.contractQuery({ pageIndex: 0, pageSize: 10 });
      }
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search {
  padding: 10px 0;
  .el-input,
  .el-select {
    width: 190px;
    height: 36px;
    margin-right: 10px;
    margin-bottom: 15px;
    opacity: 1;
    border-radius: 6px;
  }
  .el-date-editor--date {
    width: 150px;
  }
}
.ebtn {
  width: 120px;
  height: 36px;
  background: #508fcc;
  opacity: 1;
  border-radius: 24px;
  border: none;
  color: #fff;
}
.editor {
  width: 68px;
  height: 32px;
  background: #508fcc;
  opacity: 1;
  border-radius: 16px;
  margin-bottom: 20px;
}
.reset {

  background: #2ec9c7;
}
.tableHandle {
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
.btn {
  border-top: 1px solid #e6e6e6;
  padding: 20px 0;
}
.input {
  width: 150px;
}
.searchForm {
  width: 49%;
  display: inline-block;
  margin: 5px 0px;
}
.dialog {
  .dialog-title {
    color: #fff;
  }
}
</style>
